<template>
  <el-card class="attr">
    <el-form inline :disabled="disabled">
      <el-form-item label="一级分类">
        <el-select placeholder="请选择一级分类" v-model="categoryStore.category1Id">
        <el-option :label="c1.name" :value="c1.id"  v-for="c1 in categoryStore.category1List" :key="c1.id"/>
         
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类">
        <el-select placeholder="请选择二级分类" v-model="categoryStore.category2Id" >
          <el-option :label="c2.name" :value="c2.id"  v-for="c2 in categoryStore.category2List" :key="c2.id"/>
        </el-select>
      </el-form-item>

      <el-form-item label="三级分类">
        <el-select placeholder="请选择三级分类"  v-model="categoryStore.category3Id">
          <el-option :label="c3.name" :value="c3.id"  v-for="c3 in categoryStore.category3List" :key="c3.id"/>

 
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "CategorySelector",
};
</script>

<script lang="ts" setup>
import { onMounted, watch } from "vue";
import { useCategoryStore } from "@/stores/category";

defineProps<{
  disabled:boolean
}>()

const categoryStore=useCategoryStore()

onMounted(async()=>{
  categoryStore.getCategory1List()

})
watch(()=>categoryStore.category1Id,async(newVal)=>{
  categoryStore.getCategory2List(newVal as number )
})
watch(()=>categoryStore.category2Id,async(newVal )=>{
  if(!newVal)return   // 重新选择一级分类时，会将二级分类id置为undefined，此时触发watch，导致请求的二级分类id没有值，请求失败
  categoryStore.getCategory3List(newVal as number)
  
})


</script>

<style scoped>
/* 深度选择器 */
.attr :deep(.el-form-item) {
  margin-top: 10px;
  margin-bottom: 10px;
}
</style>
